package com.vess.ratingweb;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class LoginForm extends Activity {
	Button btnNext;
	WebView webView;
	private String mStrTimeStamp, mStrLogo;
	private String mStrUsername, mStrCoupon;
	private int mIntUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_form);

		mStrUsername = getIntent().getStringExtra("username");
		mStrTimeStamp = getIntent().getStringExtra("timestamp");
		mStrLogo = getIntent().getStringExtra("logo");
		mStrCoupon = getIntent().getStringExtra("coupon");
		mIntUserId = getIntent().getIntExtra("id", 0);

		webView = (WebView) findViewById(R.id.webRating);
		btnNext = (Button) findViewById(R.id.btnNext);
		String url = "http://www.yahoo.com";
		url = getIntent().getExtras().getString("url");
		WebSettings webSettings = webView.getSettings();
		webSettings.setBuiltInZoomControls(true);
		webSettings.setJavaScriptEnabled(true);
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeAllCookie();
		webView.setWebViewClient(new Callback(this, url)); // HERE IS THE MAIN CHANGE
		// webView.setWebViewClient(new WebviewCookie());
		webView.loadUrl(url);
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(LoginForm.this, webView.getUrl().toString(),
						Toast.LENGTH_LONG).show();
				/*
				 * startActivity(new Intent(Intent.ACTION_VIEW, Uri
				 * .parse(webView.getUrl())));
				 */
				if (webView.getUrl().toLowerCase()
						.contains("ServiceLoginAuth".toLowerCase())
						|| webView.getUrl().toLowerCase()
								.contains("Login".toLowerCase())) {

				} else {
					Log.d("opendiretory", "passed");
					webView.loadUrl("about:blank");
					Intent intent = new Intent(LoginForm.this, Directory.class);
					if ((mStrUsername != null) && (mStrTimeStamp != null)) {
						intent.putExtra("username", mStrUsername);
						intent.putExtra("timestamp", mStrTimeStamp);
						intent.putExtra("logo", mStrLogo);
						intent.putExtra("coupon", mStrCoupon);
						intent.putExtra("id", mIntUserId);
					}
					startActivity(intent);
				}
				// startActivity(new Intent(RatingPage.this, Result.class));
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	/*
	 * @Override protected void onActivityResult(int requestCode, int
	 * resultCode, Intent data) { super.onActivityResult(requestCode,
	 * resultCode, data); Log.d("ratpageresult", "passed"); if (resultCode ==
	 * RESULT_OK) { Log.d("ratpageresult", "passed"); Log.d("ratpageresult",
	 * data.getExtras().getString("url")); // webView.setWebViewClient(new
	 * WebviewCookie()); webView.loadUrl(data.getExtras().getString("url")); } }
	 */

	private class Callback extends WebViewClient { // HERE IS THE MAIN CHANGE.

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return (false);
		}
		
		@Override
		public void onPageFinished(WebView view, String url) {
			proDialog.dismiss();
			view.setVisibility(View.VISIBLE);
			CookieSyncManager.getInstance().sync();
			Log.d("webviewurl", webView.getUrl());
			if (webView.getUrl().toLowerCase()
					.contains("ServiceLoginAuth".toLowerCase())
					|| webView.getUrl().toLowerCase()
							.contains("Login".toLowerCase())) {
				if (webView.getUrl().toLowerCase()
						.contains("home".toLowerCase())) {
					webView.loadUrl("about:blank");
					webView.setWebViewClient(new WebviewCookie());
					Intent intent = new Intent(LoginForm.this, Directory.class);
					if ((mStrUsername != null) && (mStrTimeStamp != null)) {
						intent.putExtra("username", mStrUsername);
						intent.putExtra("timestamp", mStrTimeStamp);
						intent.putExtra("logo", mStrLogo);
						intent.putExtra("coupon", mStrCoupon);
						intent.putExtra("id", mIntUserId);
					}
					startActivity(intent);
				}

			} else {
				Log.d("opendiretory", "passed");
				webView.loadUrl("about:blank");
				webView.setWebViewClient(new WebviewCookie());
				Intent intent = new Intent(LoginForm.this, Directory.class);
				if ((mStrUsername != null) && (mStrTimeStamp != null)) {
					intent.putExtra("username", mStrUsername);
					intent.putExtra("timestamp", mStrTimeStamp);
					intent.putExtra("logo", mStrLogo);
					intent.putExtra("coupon", mStrCoupon);
					intent.putExtra("id", mIntUserId);
				}
				startActivity(intent);
			}
		}
		
		private Context mCtx;
		private ProgressDialog proDialog;
		
		public Callback(Context ctx, String strURL) {
			mCtx = ctx;
			proDialog = new ProgressDialog(mCtx);
			proDialog.setTitle(strURL);
			proDialog.setMessage("Loading...");
			proDialog.setCanceledOnTouchOutside(false);
			proDialog.show();
		}
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			view.setVisibility(View.GONE);
		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(LoginForm.this, Login.class);
		if ((mStrUsername != null) && (mStrTimeStamp != null)) {
			intent.putExtra("username", mStrUsername);
			intent.putExtra("timestamp", mStrTimeStamp);
			intent.putExtra("logo", mStrLogo);
			intent.putExtra("coupon", mStrCoupon);
			intent.putExtra("id", mIntUserId);
		}
		startActivity(intent);
		finish();
		super.onBackPressed();
	}

}
